import { Product_model } from 'product_model.js';
import {Cart_model} from '../cart/cart_model.js';

var product_model = new Product_model();
var cart_model = new Cart_model();

Page({
  data: {
    loadingHidden:false,
    productCount:1,
    countsArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  },


  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;

    this._loadData();
  },

  _loadData: function() {
    var that = this;


    product_model.read(that.data.id, (data)=>{
      that.setData({
        'product':data,
        'cartCount': cart_model.getCartCount().totalCount,
        'loadingHidden':true
      });
    });
  },


  onAddCartTap: function() {
    var data = {}, keys = ['id', 'price', 'name', 'main_image_url'];

    for (var key in this.data.product) {
      if (keys.indexOf(key) >= 0) {
          data[key] = this.data.product[key];    
      }
    }

    cart_model.add(data, this.data.productCount);
  },

  onShareAppMessage: function () {
    return {
      title: "萌店",
      desc: "朋友圈的品质生活",
      path: "pages/product/product?id=" + this.data.id 
    }
  },

  bindPickerChange: function(event) {
    this.setData({
      productCount: this.data.countsArray[event.detail.value]
    });
  },

  //显示tabs
  onTabsItemTap: function(event) {
    var index = product_model.getDataSet(event, 'index');
    this.setData({
      'currentTabsIndex':index
    });
  }

})